package com.paohe.lianxin_pda.utils.spinner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.paohe.lianxin_pda.R;

import java.util.ArrayList;

public class SpinnerUtils {
    private Context mContext;
    private TextView mTextView;
    private String[] mData;
    private MyItemClickListener itemClickListener;

    public interface MyItemClickListener{
        void onClick(View view);
    }

    public SpinnerUtils(Context context , TextView textView , String[] data, MyItemClickListener itemClickListener) {
        this.mContext = context;
        this.mTextView = textView;
        this.mData = data;
        this.itemClickListener = itemClickListener;
    }

    public void showPopupWindow() {
        tvSetImg(mTextView, R.mipmap.top_arrow);
        View view = LayoutInflater.from(mContext).inflate(R.layout.choose_pop, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_popup_view));
        popupWindow.showAsDropDown(mTextView);
        popupWindow.setOnDismissListener(new PopupDismissListener());
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_choose_pop);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        SpinnerChooseAdapter adapter = new SpinnerChooseAdapter(mContext, new SpinnerChooseAdapter.MyItemClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onClick(view);
                int tag = (int) view.getTag();
                mTextView.setText(mData[tag]);
                popupWindow.dismiss();
            }
        }, mData);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
    }

    /**
     * 弹窗消失的时候让箭头换回来
     */
    class PopupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            tvSetImg(mTextView, R.mipmap.down_arrow);
        }

    }

    /**
     * 设置textView右侧的图像
     *
     * @param textView
     * @param img
     */
    private void tvSetImg(TextView textView, int img) {
        Drawable nav_up = mContext.getResources().getDrawable(img);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        textView.setCompoundDrawables(null, null, nav_up, null);
    }
}
